package com.zw.mizhe.view.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.CountDownTimer;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.zw.mizhe.R;
import com.zw.mizhe.common.base.BaseActivity;
import com.zw.mizhe.common.image.ImageLoader;

import java.io.File;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 广告
 */
public class AdActivity extends BaseActivity {
    SharedPreferences sp;
    @BindView(R.id.ad_img)
    ImageView adImg;
    @BindView(R.id.timer_btn)
    Button timerBtn;
    private String imagePath;
    AdCountDownTimer timer;

    @Override
    public void before() {
        super.before();
        sp = PreferenceManager.getDefaultSharedPreferences(this);
        imagePath = sp.getString(SplashActivity.FIRST_AD_PATH_PREFS, "");
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_adv;
    }

    @Override
    public void initView() {
        timer = new AdCountDownTimer(4000, 1000);
        timer.start();
        if (!TextUtils.isEmpty(imagePath)) {
            ImageLoader.getInstance().displayImage(this, new File(imagePath), adImg);
        }
    }

    @OnClick(R.id.timer_btn)
    public void onClick(View view) {
        startActivities();
    }

    public class AdCountDownTimer extends CountDownTimer {

        String timeStr;

        public AdCountDownTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);

        }

        @Override
        public void onTick(long millisUntilFinished) {
            timeStr = millisUntilFinished / 1000 + "s 跳转主界面";
            timerBtn.setText(timeStr);
        }

        @Override
        public void onFinish() {
            timeStr = "0s 跳转主界面";
            timerBtn.setText(timeStr);
            startActivities();

        }

    }

    public void startActivities() {
        startActivity(new Intent(AdActivity.this, MainActivity.class));
        onBackPressed();
    }


}
